<?php

Raise::load('core.helper.convert.RaiseAbstractConverter');
Raise::load('core.geometry.RaiseRectangle');

/**
 * RaiseRectangle Converter helper class
 * for conversion to RaiseRectangle
 * 
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.helper.convert.datatype
 * @since 1.2
 */
class RaiseRectangleConverter extends RaiseAbstractConverter {
    
    /**
     * Convert the subject to RaiseRectangle
     * @param mixed $subject The subject to convert
     * @return RaiseRectangle The result
     */
    public function convert($subject) {
        if(RaiseVariable::isType($subject, array('RaiseCollection', RaiseVariable::TYPEARRAY))){
            return new RaiseRectangle(new RaisePoint(RaiseConvert::to(RaiseConvert::TO_DOUBLE, $subject[0]), RaiseConvert::to(RaiseConvert::TO_DOUBLE, $subject[1])), RaiseConvert::to(RaiseConvert::TO_DOUBLE, $subject[2]), RaiseConvert::to(RaiseConvert::TO_DOUBLE, $subject[3]));
        }elseif(RaiseVariable::isType($subject, RaiseVariable::TYPESTRING)){
            // parse strings like "20, 40"
            $v = explode(',', $subject);
            if(count($v) == 4){
                return new RaiseRectangle(RaiseConvert::to(RaiseConvert::TO_DOUBLE, trim($v[0])), RaiseConvert::to(RaiseConvert::TO_DOUBLE, trim($v[1])), RaiseConvert::to(RaiseConvert::TO_DOUBLE, trim($v[2])), RaiseConvert::to(RaiseConvert::TO_DOUBLE, trim($v[3])));
            }
        }
        return new RaiseRectangle(RaisePoint::zero(), 0, 0);
    }
    
}